<?php


namespace campus\controllers;


use common\helpers\commonApi;
use common\models\Classify;
use common\models\Goods;
use common\models\service\GoodsStockService;
use Yii;

/**
 * 商品管理
 * Class GoodsController
 * @package backend\controllers
 */
class GoodsController extends BaseController
{
    public function init()
    {
        parent::init();
        $this->model = Goods::className();
    }

    /**
     * 列表
     * @return array|string
     */
    public function actionIndex()
    {
        if (Yii::$app->request->isPost){
            $data = parent::actionIndex(true,[['delete','=',0]]);
            $classify_id = array_column($data['data'],'classify_id');
            $classify = Classify::getAll(['in','id',$classify_id]);
            $classify = array_column($classify,'name','id');
            foreach ($data['data'] as &$datum){
                $datum['classify'] = $classify[$datum['classify_id']]??'';
                $datum['operlog'] = commonApi::getOperLog($datum);
            }
            return $data;
        }
        $data = Classify::getAll();
        Yii::$app->view->params['data'] = $data;
        return parent::actionIndex();
    }

    public function actionSpec()
    {
        if (Yii::$app->request->isPost){
            $data = Yii::$app->request->post();
            $specAll = $data['specAll']??[];
            return GoodsStockService::setSpec($specAll);
        }
        $id = Yii::$app->request->get('id','');
        $data = GoodsStockService::getSpec($id,$this->campus_id);
        return $this->render('',['data'=>$data,'id'=>$id]);
    }
}